package com.nuts.core.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nuts.base.bean.ResultVO;
import com.nuts.base.core.ResultGenerator;
import com.nuts.base.core.annotation.Permission;
import com.nuts.base.core.annotation.PermissionPath;
import com.nuts.core.bean.request.DimensionItemDataReqVO;
import com.nuts.core.bean.response.DimensionItemDataVO;
import com.nuts.core.service.IDimensionItemDataService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 维度数据范围控制器
 * 
 * @author 许自辉
 * @date 2017年8月21日
 */

@SpringBootConfiguration
@RestController
@RequestMapping("api/cloud")
@PermissionPath("系统管理/维度/数据范围/范围数据")
@Api(value = "dimensionItemData-api", tags = { "维度相关接口" })
public class DimensionItemDataController {

	@Autowired
	private IDimensionItemDataService iDimensionItemDataService;

	@ApiOperation("分页查询数据范围数据")
	@Permission("dimensionItemData:findPage")
	@GetMapping("dimensionItemData/findPage")
	public ResultVO getDimensionItemDataPage(@RequestParam("pageNum") int pageNum,
			@RequestParam("pageSize") int pageSize, DimensionItemDataReqVO dataReqVO) {
		PageHelper.startPage(pageNum, pageSize);
		List<DimensionItemDataVO> dimensionItemVOs = iDimensionItemDataService.getList(dataReqVO);
		return ResultGenerator.getSuccess(new PageInfo<DimensionItemDataVO>(dimensionItemVOs));
	}

	@ApiOperation("批量添加数据范围数据")
	@Permission("dimensionItemData:createList")
	@PostMapping("dimensionItemData/createList")
	public ResultVO createDimensionItemDataList(@RequestBody @Validated List<DimensionItemDataReqVO> dataReqVOs) {
		int count = iDimensionItemDataService.insertList(dataReqVOs);
		if (count == -1) {
			return ResultGenerator.getFail("该数据范围已经存在！");
		}
		return ResultGenerator.getInsertSuccess(count);
	}

	@ApiOperation("批量删除数据范围数据")
	@Permission("dimensionItemData:deleteList")
	@DeleteMapping("dimensionItemData/deleteList")
	public ResultVO deleteDimensionItemData(@ApiParam(value = "维度 id集合") @RequestBody int[] ids) {
		int count = iDimensionItemDataService.deleteList(ids);
		return ResultGenerator.getDeleteSuccess(count);
	}
}
